package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/10 9:49
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired(required = false)
    private CategoryService categoryService;

    @Autowired(required = false)
    private HttpSession session;

    /**
     * 添加分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category,session);
        return R.success("添加成功");
    }

    /**
     * 显示分类数据
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> findByPage(Integer page,Integer pageSize){
        Page<Category> categoryPage = categoryService.findAll(page, pageSize);
        return R.success(categoryPage);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long id){
        categoryService.DeleteById(id);
        return R.success("删除成功");
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Category> findById(@PathVariable Long id){
        Category category = categoryService.findById(id);
        return R.success(category);
    }


    /**
     * 更改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.update(category,session);

        return R.success("修改成功");
    }

    /**
     * 导出Excel
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        response.setHeader("content-disposition","attachment;filename=category.xlsx");

        //1. 读取模板的输入流
        InputStream inputStream = this.getClass().getResourceAsStream("/excel/category.xlsx");

        //2。 使用模板的输入流构建一个工作薄
        Workbook workbook = new XSSFWorkbook(inputStream);
        //Workbook  workbook= new HSSFWorkbook(inputStream);

        //3. 得到工作单
        Sheet sheet = workbook.getSheetAt(0);

        //4. 读取第一行,提取第一行的样式，并且存储到集合中
        Row row = sheet.getRow(1);
        List<CellStyle> cellStylesList= new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            //得到单元格的样式
            CellStyle cellStyle = row.getCell(i).getCellStyle();
            cellStylesList.add(cellStyle);
        }

        //5. 得到数据,遍历数据，把数据写入到excel
        List<Category> categoryList = categoryService.findAll();
        for (int i = 0; i < categoryList.size(); i++) {
            //一个类别就对应一行
            row =  sheet.createRow(i+1);
            Category category = categoryList.get(i);
            //把类别信息设置到单元格上

            //类别类型
            Cell cell = row.createCell(0);
            //设置单元格内容
            cell.setCellValue(category.getType());
            cell.setCellStyle(cellStylesList.get(0));

            //类别名字
            cell = row.createCell(1);
            //设置单元格内容
            cell.setCellValue(category.getName());
            cell.setCellStyle(cellStylesList.get(1));


            //类别排序
            cell = row.createCell(2);
            //设置单元格内容
            cell.setCellValue(category.getSort());
            cell.setCellStyle(cellStylesList.get(2));

            //创建时间
            cell = row.createCell(3);
            //设置单元格内容
            cell.setCellValue(category.getCreateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(3));


            //创建时间
            cell = row.createCell(4);
            //设置单元格内容
            cell.setCellValue(category.getUpdateTime().toLocalDate().toString());
            cell.setCellStyle(cellStylesList.get(4));
        }
        //6. 把工作薄写出
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> categoryList = categoryService.findByType(type);

        return R.success(categoryList);
    }
}
